# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=cppwinrt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.221121.5
pkgrel=2
pkgdesc="C++ language projection for Windows Runtime (WinRT) APIs (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clang32')
url='https://aka.ms/cppwinrt'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/microsoft/cppwinrt/archive/refs/tags/${pkgver}.tar.gz"
        https://github.com/microsoft/cppwinrt/pull/1235.patch)
sha256sums=('1b8de90c23cbc04d250a8c5bc263819d8a84fef2a5f5054229a21c7d711487c2'
            '9802c40d1c6a77d07209e0fa52c935a69054a10ea8e72c137b466f448b6746b6')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/1235.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe -Wno-dev \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DCPPWINRT_BUILD_VERSION="${pkgver}" \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe -DBUILD_TESTING=ON ../${_realname}-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  "${MINGW_PREFIX}"/bin/ctest.exe . || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 cppwinrt.exe "${pkgdir}${MINGW_PREFIX}/bin/cppwinrt.exe"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
